//
//  KKSearchResultVC.m
//  DoMi
//
//  Created by 康凯 on 2016/12/1.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKSearchResultVC.h"
#import "KKVideo1Cell.h"
#import "KKVideoDetailController.h"
#import "KKStudyHttp.h"
#import "KKArticleDetailVC.h"
#import "KKSearchM.h"

@interface KKSearchResultVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *searchInput;
@property (weak, nonatomic) IBOutlet UIView *itemContainer;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong,nonatomic) NSArray *titles;
@property (strong,nonatomic) UIButton *tempBtn;
@property (copy,nonatomic) NSString *type;
@property (strong,nonatomic) NSArray *resultDatas;
@end

@implementation KKSearchResultVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBlack];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav];
    [self initView];
    // Do any additional setup after loading the view from its nib.
}

#pragma mark ---初始化nav
-(void)initNav{
    //返回字体颜色
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    self.navigationItem.title=@"搜索结果";
}

-(void)initView{
//    self.titles=@[@"全部",@"视频",@"文章",@"其他",];
    self.titles=@[@"全部",@"视频",@"文章",];
    self.type=@"all";
    self.searchInput.text=self.searchContent;
    
    for(int i=0;i<self.titles.count;i++){
        float itemWidth=screenWidth/self.titles.count;
        UIButton *item=[[UIButton alloc] initWithFrame:CGRectMake(i*itemWidth, 0, itemWidth, 30)];
        if(i==0){
            self.tempBtn=item;
        }
        item.selected=i==0?YES:NO;
        item.tag=i;
        [item setTitle:self.titles[i] forState:UIControlStateNormal];
        [item setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [item setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        item.titleLabel.font=[UIFont systemFontOfSize:14];
        [item addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.itemContainer addSubview:item];
    }

    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self getSearchList];

}

#pragma mark 头部item点击
-(void)btnClick:(UIButton *)btn{
    self.tempBtn.selected=NO;
    btn.selected=YES;
    self.tempBtn=btn;
    switch (btn.tag) {
        case 0:
            self.type=@"all";
            break;
        case 1:
            self.type=@"v";
            break;
        case 2:
            self.type=@"w";
            break;
        case 3:
            self.type=@"other";
            break;
    }
    [self getSearchList];
}

#pragma mark tableview代理
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    KKSearchM *search= self.resultDatas[indexPath.row];
    KKVideo1Cell *video=(KKVideo1Cell *)cell;
    [video initSearchData:search];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.resultDatas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KKVideo1Cell *cell= [tableView dequeueReusableCellWithIdentifier:@"KKVideo1Cell"];
    if(!cell){
        cell=[[[NSBundle mainBundle] loadNibNamed:@"KKVideo1Cell" owner:nil options:nil] lastObject];
    }
    cell.backgroundColor=[UIColor colorWithHexString:@"#EAEBEC"];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KKSearchM *search= self.resultDatas[indexPath.row];
    if([search.type isEqualToString:@"w"]){
        KKArticleDetailVC *detail=[[KKArticleDetailVC alloc] init];
        if([self.type isEqualToString:@"all"]){
            detail.docId=search.id;
        }else{
            detail.docId=search.articleid;
        }
        [self.navigationController pushViewController:detail animated:YES];
    }else if([search.type isEqualToString:@"v"]){
        KKVideoDetailController *detail=[[KKVideoDetailController alloc]init];
        detail.videoId=search.id;
        [self.navigationController pushViewController:detail animated:YES];
    }
    
}

#pragma mark 按钮--搜索
- (IBAction)searchClick:(id)sender {
    [self getSearchList];
}

#pragma mark 接口--排行榜数据
-(void)getSearchList{
    [self showDialog];
    NSDictionary *params=@{UID_KEY:UID,TOKEN_KEY:TOKEN,@"type":self.type,@"keyword":self.searchInput.text};
    [KKStudyHttp getSearchList:params success:^(id datas) {
        self.resultDatas=datas;
        [self.tableView reloadData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
